Skip to content

Strip a repeated character from both ends in one pass - #403

Open
adityawrk wants to merge 2 commits into
ruby:mainfrom
adityawrk:fix/linear-strip
Open

adityawrk wants to merge 2 commits into
ruby:mainfrom
adityawrk:fix/linear-strip

Conversation

@adityawrk

Copy link
Copy Markdown

Fixes #396.

strip: " " with quote_char: nil removed the padding one character at a time. Each delete_prefix! copied the rest of the line, so a long run of the same character grew with the square of its length. On this machine, 400,000 leading spaces took about 1.1 seconds. strip: true was already a single String#strip!, and a quoted field was already stripped by a regexp inside the scanner. This path is the one that loops.

The string strip is now one regexp anchored at both ends, built once when the parser is prepared. The same 400,000-character pad finishes in about 5 milliseconds, and the field value is unchanged.

The regression test parses short padded lines, including a multibyte strip character, and times strip_value directly. Timing the whole parse would be dominated by the one-byte reads used when CSV_PARSER_SCANNER_TEST is set. The old loop takes about a second at this size, so the 0.25 second bound fails if the loop comes back.

quote_char: nil and strip: " " removed each padding character
with delete_prefix!, which copies the rest of the string.
A 400_000-character pad took about a second.
delete_prefix! in a loop copied the remainder for every
leading character. A single regexp removes both ends.

Fixes ruby#396

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Quadratic CPU cost when stripping a long padded field with a string strip option

1 participant